using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;


public class NPC_Editor : EditorWindow {
    private static List<NPC_Stats> npcs;
	private static Vector2 scrollVector;
	private static Vector2 scrollVector2;
	private static string[] toolbarStrings  = {"Load","Save","Add", "Delete"};
	private static int selLabel=-1;
	private static SerializedProperty DamageProp1;
	private static GameObject Npc_Obj;
	
	private static bool expanded =false;
	private static Spell_Power_Type opRes = Spell_Power_Type.Physical;
	
	private static bool expanded2 =false;
	private static Spell_Power_Type opDmg = Spell_Power_Type.Physical;	
	
	private static bool expanded3 =false;
	private static string[] _spells;
	private static int opSpell=0;
	
    [MenuItem ("Ciber Tools/NPC Editor")]
   static void Init () {
        NPC_Editor window = (NPC_Editor)EditorWindow.GetWindow( typeof(NPC_Editor) );
		npcs = new List<NPC_Stats>();
		selLabel=-1;
		_spells = IOClass.GetAllDerivedST(typeof(Base_Spell));
    }
    void OnGUI () {
		Show_List_NPC();
		Show_NPC_Details();
		Show_Toolbar();
    }
	
	static void Show_List_NPC()
	{
		GUILayout.BeginVertical(GUI.skin.box);
      	scrollVector = GUILayout.BeginScrollView(scrollVector);
		GUILayout.Label("NPC Names", EditorStyles.boldLabel);
		if(npcs !=null){
			string[] names = new string[npcs.Count];
			for (int i = 0; i < npcs.Count; i++)
		    {
				names[i]=npcs[i].Name;
		    }
			int oldselLabel = selLabel;
			selLabel = GUILayout.SelectionGrid(selLabel,names,1);
			if(oldselLabel!=selLabel)
			{
				Npc_Obj = (GameObject)AssetDatabase.LoadAssetAtPath(npcs[selLabel].Object_Name,typeof(GameObject));
			}
		}
	    GUILayout.EndScrollView();
	    GUILayout.EndVertical();
	}
	static void Show_NPC_Details()
	{
		int i = selLabel;
		GUILayout.BeginHorizontal(GUI.skin.box);
		GUILayout.Label("       NPC Details       ", EditorStyles.miniBoldLabel);
		if(Npc_Obj != null)
		{
			Rect trect = GUILayoutUtility.GetLastRect();
			trect.x += 5;
			trect.y += 25;
			trect.height = 100;
			trect.width  = 100;

			EditorGUI.DrawPreviewTexture(trect,
			AssetPreview.GetAssetPreview(Npc_Obj));
		}
		if(selLabel>-1){
	      	scrollVector2 = GUILayout.BeginScrollView(scrollVector2);
				
			GUILayout.Label("ID: "); npcs[i].Id = int.Parse(GUILayout.TextField(npcs[i].Id.ToString()));
	        GUILayout.Label("Name: "); npcs[i].Name = GUILayout.TextField(npcs[i].Name);
	
	    	npcs[i].Life_Cap = (int) EditorGUILayout.IntSlider ("Life", npcs[i].Life_Cap, 0, 5000);
	    	ProgressBar (((float)npcs[i].Life_Cap) / 5000.0f, "Life");
			
	    	npcs[i].Energy_Cap = (int) EditorGUILayout.IntSlider ("Energy", npcs[i].Energy_Cap, 0, 5000);
	    	ProgressBar (((float)npcs[i].Energy_Cap) / 5000.0f, "Energy");
			
			Npc_Obj =(GameObject) EditorGUILayout.ObjectField(Npc_Obj, typeof(GameObject));
			if(Npc_Obj != null)
				npcs[i].Object_Name = AssetDatabase.GetAssetPath( Npc_Obj );
			
			Resistances(i);
			GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); 
			GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); 
			Damages(i);
			GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); 
			GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); 
			Spells(i);
			GUILayout.EndScrollView();
	    }
		GUILayout.EndHorizontal();
	}
	static void Show_Toolbar()
	{
		GUILayout.BeginVertical(GUI.skin.box);
		int pushed = GUILayout.Toolbar( -1 ,toolbarStrings);
		switch (pushed) {
			case 0: Load(); break;
			case 1: Save(); break;
			case 2: AddNPC(); break;
			case 3: DelNPC(); break;
		}
		GUILayout.EndVertical();
	}

	
	static void Save()
	{
		//EditorUtility.DisplayDialog("Info","Save","Ok");
		string filename = EditorUtility.SaveFilePanel("Save NPC Collection",Application.dataPath+"/GData","","gdt");
		if(filename=="")
			return;
		using(var sm = File.Create(filename))
		{
			foreach(NPC_Stats n in npcs)
			{
				n.Save(sm);
				Debug.Log("Save - " + sm.Position.ToString() + " / " + sm.Length.ToString() );
			}
		}
	}
	static void Load()
	{
		//EditorUtility.DisplayDialog("Info","Save","Ok");
		string filename = EditorUtility.OpenFilePanel("Load NPC Collection",Application.dataPath+"/GData","gdt");
		if(filename=="")
			return;
		using(var sm = File.OpenRead(filename))
		{
			npcs.Clear();
			while((sm.Position < sm.Length ))
			{
				NPC_Stats n = new NPC_Stats();
				n.Load(sm);
				npcs.Add(n);
				Debug.Log("Load - " + sm.Position.ToString()  + " / " + sm.Length.ToString() );
			}
		}
	}
	static void AddNPC()
	{
		//EditorUtility.DisplayDialog("Info","AddNPC","Ok");
	 	NPC_Stats npc = new NPC_Stats();
		npc.Id = npcs.Count+1;
		npc.Name = "New NPC "+npc.Id.ToString();
		npc.Life_Cap = 100;
		npc.Energy_Cap = 100;
		npcs.Add(npc);
	}
	static void DelNPC()
	{
		EditorUtility.DisplayDialog("Info","DelNPC","Ok");
	}
	static void ProgressBar (float val , string label) 
	{
        Rect rect  = GUILayoutUtility.GetRect (18, 18, "TextField");
        EditorGUI.ProgressBar(rect, val, label);
        EditorGUILayout.Space ();
    }
	static void Resistances(int i)
	{
		expanded = EditorGUILayout.Foldout( expanded, "Resistances" );
		  if( expanded )
		  {
			opRes = (Spell_Power_Type)EditorGUILayout.EnumPopup("Add Resistance:",opRes);
			if(GUILayout.Button("Add"))
			    AddRes(opRes, npcs[i]);
			List<int> remover = new List<int>();
			
			int p=0;
			foreach(int g in npcs[i].Resistance_Values)
			{
				string rn = Enum.GetName(typeof(Spell_Power_Type), npcs[i].Resistances_Types[p]);
				if (GUILayout.Button("X", EditorStyles.miniButton, GUILayout.Width(25)))
				{
					remover.Add(p);
				}
				npcs[i].Resistance_Values[p] = EditorGUILayout.IntSlider(rn, npcs[i].Resistance_Values[p], 0, 1000);
    			ProgressBar (((float)npcs[i].Resistance_Values[p]) / 1000.0f, rn);
				p++;
			}
			
			if(remover.Count >0)
			{
				for(int g=0; g<remover.Count; g++)
				{
					npcs[i].Resistance_Values.RemoveAt(remover[g]);
					npcs[i].Resistances_Types.RemoveAt(remover[g]);
				}
			}
		  }
	}
    static void AddRes(Spell_Power_Type op, NPC_Stats npc) {
		npc.Resistances_Types.Add(op);
		npc.Resistance_Values.Add(1);
      }
	static void Damages(int i)
	{
		expanded2 = EditorGUILayout.Foldout( expanded2, "Damage" );
		  if( expanded2 )
		  {
			opDmg = (Spell_Power_Type)EditorGUILayout.EnumPopup("Add Dmg type:",opDmg);
            if(GUILayout.Button("Add"))
                AddDmg(opDmg, npcs[i]);
			
			List<int> remover = new List<int>();			
			int p=0;
			foreach(int g in npcs[i].Dmg_Values)
			{
				string rn = Enum.GetName(typeof(Spell_Power_Type), npcs[i].Dmg_Types[p]);
				if (GUILayout.Button("X", EditorStyles.miniButton, GUILayout.Width(25)))
				{
					remover.Add(p);
				}
				npcs[i].Dmg_Values[p] = EditorGUILayout.IntSlider(rn, npcs[i].Dmg_Values[p], 0, 1000);
    			ProgressBar (((float)npcs[i].Dmg_Values[p]) / 1000.0f, rn);
				p++;
			}

			if(remover.Count >0)
			{
				for(int g=0; g<remover.Count; g++)
				{
					npcs[i].Dmg_Types.RemoveAt(remover[g]);
					npcs[i].Dmg_Values.RemoveAt(remover[g]);
				}
			}
			
		  }
	}
    static void AddDmg(Spell_Power_Type op, NPC_Stats npc) {
		npc.Dmg_Types.Add(op);
		npc.Dmg_Values.Add(1);
      }
	
	static void Spells(int i)
	{
		expanded3 = EditorGUILayout.Foldout( expanded3, "Spells" );
		  if( expanded3 )
		  {
			opSpell = EditorGUILayout.Popup(opSpell, _spells);
			if(GUILayout.Button("Add"))
			    npcs[i].Spell_Name.Add(_spells[opSpell]);
			
			List<int> remover = new List<int>();
			
			int p=0;
			foreach(string g in npcs[i].Spell_Name)
			{
				if (GUILayout.Button("X", EditorStyles.miniButton, GUILayout.Width(25)))
				{
					remover.Add(p);
				}

				EditorGUILayout.LabelField("Spell "+(p+1).ToString()+": ", g);
				p++;
			}
			
			if(remover.Count >0)
			{
				for(int g=0; g<remover.Count; g++)
				{
					npcs[i].Spells.RemoveAt(remover[g]);
				}
			}
	
		}
	}
}